package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/checkgroup")
@RestController
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 新增检查组
     * @param group
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup group ,Integer[] checkitemIds){
        log.info("新增检查组:{}",group,checkitemIds);
        checkGroupService.add(group,checkitemIds);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询所有检查组
     */
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("查询所有检查组");
        List<CheckGroup> checkGroup =  checkGroupService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }

    /**
     * 分页查询检查组
     */
    @PostMapping("/findPage")
    public PageResult page(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询检查组:{}",queryPageBean);
        PageResult pageResult = checkGroupService.page(queryPageBean);
        return pageResult;
    }

    /**
     * 删除检查组
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public Result delete(Integer id){
        log.info("删除检查组:{}",id);
        checkGroupService.delete(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }
    /**
     * 根据id查询检查组
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("根据id查询检查组:{}",id);
        CheckGroup checkGroup = checkGroupService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }

    /**
     * 通过套餐id查询检查组id
     * @param
     * @return
     */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer setmealId){
        log.info("通过套餐id查询检查组id:{}",setmealId);
        List<Integer> checkGroupIds = checkGroupService.findCheckGroupIdBysetmealId(setmealId);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupIds);
    }

    /**
     * 编辑检查组
     * @param group
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup group,Integer[] checkitemIds){
        log.info("编辑检查项:{}",group,checkitemIds);
        checkGroupService.edit(group,checkitemIds);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }
}
